package de.preamp.clients.java.data;

import java.awt.datatransfer.DataFlavor;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.swing.ImageIcon;

/**
 * This class represents a track from the database. It provides root information
 * for creating albums and artists they belong to. All of it's fields containing
 * meta information should be valid an consistent to the id3 tags of the
 * represented file in the database.
 * 
 * @author André Stannek
 * @version 2010-07-30
 * @see AbstractDBItem
 * @see Album
 * @see Artist
 */
public class Track extends AbstractDBItem implements Comparable<Track>
{
    /**
     * The unique id (primary key) of the media file in the database.
     */
    private final transient String dbID;

    /**
     * The artist information from the id3v2.4 TPE1 field.
     */
    private final transient String artist;

    /**
     * The title information from the id3v2.4 TIT2 field.
     */
    private final transient String title;

    /**
     * The album information from the id3v2.4 TALB field.
     */
    private final transient String album;

    /**
     * The track number information from the id3v2.4 TRCK field.
     */
    private final transient int trackNo;

    /**
     * The rating information from the id3v2.4 POPM rating field.
     */
    private final transient short rating;

    /**
     * The length of the track from the id3v2.4 TLEN field.
     */
    private final transient int length;

    /**
     * The http link to represented file on the server.
     */
    private final transient URL link;

    /**
     * @param dbID
     *            The unique id (primary key) of the media file in the database.
     * @param artist
     *            The artist information from the id3v2.4 TPE1 field.
     * @param title
     *            The title information from the id3v2.4 TIT2 field.
     * @param album
     *            The album information from the id3v2.4 TALB field.
     * @param track
     *            The track number information from the id3v2.4 TRCK field.
     * @param rating
     *            The rating information from the id3v2.4 POPM rating field.
     * @param length
     *            The length of the Track from the id3v2.4 TLEN field.
     * @param link
     *            The http link to represented file on the server
     * @throws MalformedURLException
     *             if link is wrong
     */
    public Track(final String dbID, final String artist, final String title,
	    final String album, final int track, final short rating,
	    final int length, final String link) throws MalformedURLException
    {
	super();
	this.dbID = dbID;
	this.artist = artist;
	this.title = title;
	this.album = album;
	trackNo = track;
	this.rating = rating;
	this.length = length;
	this.link = new URL(link.replaceAll(" ", "%20"));
    }

    /**
     * @return The unique id (primary key) of the media file in the database
     */
    public final String getId()
    {
	return dbID;
    }

    /**
     * @return The artist information from the id3v2.4 TPE1 field
     */
    public final String getArtist()
    {
	return artist;
    }

    /**
     * @return The title information from the id3v2.4 TIT2 field
     */
    public final String getTitle()
    {
	return title;
    }

    /**
     * @return The album information from the id3v2.4 TALB field
     */
    public final String getAlbum()
    {
	return album;
    }

    /**
     * @return The track number information from the id3v2.4 TRCK field
     */
    public final int getTrack()
    {
	return trackNo;
    }

    /**
     * @return The rating information from the id3v2.4 POPM rating field
     */
    public final short getRating()
    {
	return rating;
    }

    /**
     * @return The http link to represented file on the server
     */
    public final URL getURL()
    {
	return link;
    }

    /**
     * @return the length of this track due to id3 TLEN field
     */
    public final int getLength()
    {
	return length;
    }

    /**
     * Returns a string representation of the length of this track due to id3
     * TLEN field.
     * 
     * @return the length of this track as String
     */
    public final String getLengthAsString()
    {
	final int overallSeconds = length / 1000;
	final int seconds = overallSeconds % 60;
	final int minutes = overallSeconds / 60;
	final StringBuilder time = new StringBuilder();
	time.append(minutes);
	time.append(":");
	if (seconds < 10)
	{
	    time.append(0);
	}
	time.append(seconds);
	return time.toString();
    }

    /**
     * @return the Cover for the album this track belongs to, from the id3
     *         APIC$03 field.
     */
    public final ImageIcon getCover()
    {
	// TODO Cover of a single track
	return null;
    }

    @Override
    public final int compareTo(final Track object)
    {
	return toString().compareTo(object.toString());
    }

    @Override
    public final int hashCode()
    {
	return dbID.hashCode();
    }

    @Override
    public final boolean equals(final Object object)
    {
	boolean result = false;
	if (object instanceof Track)
	{
	    final Track track = (Track) object;
	    if (dbID.equals(track.dbID))
	    {
		result = true;
	    }
	}
	return result;
    }

    @Override
    public final String toString()
    {
	final StringBuilder result = new StringBuilder();
	if (trackNo < 10)
	{
	    result.append(0);
	}
	result.append(trackNo);
	result.append(" - ");
	result.append(title);
	return result.toString();
    }

    @Override
    public final DataFlavor[] getTransferDataFlavors()
    {
	return new DataFlavor[]{ new DataFlavor(
		DataFlavor.javaJVMLocalObjectMimeType + "; class="
			+ getClass().getName(), "Track") };
    }

    @Override
    public final SortedSet<Track> getTracks()
    {
	final TreeSet<Track> result = new TreeSet<Track>();
	result.add(this);
	return result;
    }
}
